/*
 * BanBu_AvatarSelectViewController
 * @description 本文件提供视频认证上传真实头像界面的ViewController
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供视频认证上传真实头像界面的ViewController
 */

#import "BanBu_AvatarSelectViewController.h"
#import "UIViewController+Animation.h"
#import "AppDataManager.h"
#import "BanBu_VideoVerifyViewController.h"
@interface BanBu_AvatarSelectViewController ()
{
    BOOL isReadyNext;
    UIImage *uploadImage;
}
@end

@implementation BanBu_AvatarSelectViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}
-(id)initWithRegisterVerify:(BOOL)_flag
{
    self = [super init];
    if(self)
    {
        isRegisterVerify = _flag;
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = NSLocalizedString(@"videoVerify", nil);
    NSString *selfSex = @"";
//    [[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"gender"];
    selfSex = @"m";
    if([selfSex isEqualToString:@"m"])
    {
        [avatarButton setImage:[UIImage imageNamed:@"mSelect.png"] forState:UIControlStateNormal];
    }
    else
    {
        [avatarButton setImage:[UIImage imageNamed:@"fSelect.png"] forState:UIControlStateNormal];
    }
//    [avatarButton setBackgroundImage:[UIImage imageNamed:@"logo.png"] forState:UIControlStateNormal];

    noticeLabel.text = NSLocalizedString(@"selectPhotoNoticeLabel", nil);
    
    [nextButton setTitle:NSLocalizedString(@"nextButton", nil) forState:UIControlStateNormal];
    
    isReadyNext = NO;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(IBAction)selectPhotoUpload:(id)sender
{
    [self selectPhoto];
}
-(void)viewWillAppear:(BOOL)animated
{
    [UIView setAnimationsEnabled:YES];
    [super viewWillAppear:animated];
}
-(IBAction)next:(id)sender
{
    if(!isReadyNext)
    {
        [self selectPhoto];
        return;
    }
    BanBu_VideoVerifyViewController *p = [[BanBu_VideoVerifyViewController alloc] initWithRegisterVerify:isRegisterVerify andParentViewController:self];
    [self presentViewController:p animated:YES];
    p.uploadImage = uploadImage;
    [p release];
}

#pragma mark - 选择视频认证照片
-(void)selectPhoto
{
    UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"selfImage", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"取消", nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"funLabel", nil),NSLocalizedString(@"btnXiangCe", nil), nil];
    [actionsheet showInView:self.view];
    [actionsheet release];
    actionsheet = nil;
    
}
    
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0)
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        if ([UIImagePickerController isSourceTypeAvailable:
             UIImagePickerControllerSourceTypeCamera])
        {
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//            picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        }
        [picker setAllowsEditing:YES];
        [self presentViewController:picker animated:YES];
        [picker release];
        picker = nil;
    }
    else if(buttonIndex == 1)
    {
        UIImagePickerController * picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        [picker setAllowsEditing:YES];
        [self presentViewController:picker animated:YES];
        [picker release];
        picker = nil;
    }
    else
    {
        
    }
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSLog(@"%@",info);
//    UIImagePickerControllerOriginalImage
    uploadImage = [info valueForKey:UIImagePickerControllerEditedImage];
    [avatarButton setImage:uploadImage forState:UIControlStateNormal];
    isReadyNext = YES;
    [picker dismissViewControllerAnimated:YES];
    
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES];
}
@end
